---
title: "Racism density plots"
author: 
  - ""
date: ""
output:
  html_document:
    df_print: paged
    toc: yes
    toc_depth: 3
    toc_float: yes
    theme: paper
editor_options:
  chunk_output_type: inline
---


```{r about, echo=FALSE, results = "asis"}
cat(
  sprintf("Updated: %s.", 
          format(Sys.time(), "%b %d, %Y at %H:%M:%S", usetz = TRUE)),
  sprintf("Working directory: `%s`.", getwd()),
  sep = "\n\n"
)
```


Reads:

  - FILEPATH

Writes:

  - FILEPATH

# Setup

```{r packages, warning=FALSE, message=FALSE}
rm(list = ls())

library(ggplot2)
library(haven)
library(dplyr)
library(questionr)
library(tidylog)
library(ggridges)

theme_set(cowplot::theme_cowplot())

base_path <- "/home/groups/jgrimmer/trump_blocs"
```

```{r warning = FALSE, message = FALSE}
d <- file.path(base_path, "replication/data/OutputData_2.dta") %>% 
        haven::read_dta()
anes <- readRDS(file.path(base_path, "data/anes_clean.rds"))
cces <- readRDS(file.path(base_path, "data/cces_clean.rds"))
```


```{r warning = FALSE, message = FALSE}
valetal <- 
  sprintf("/home/groups/jgrimmer/trump_blocs/data/valentino/study%s.dta",
          1:4) %>% 
  lapply(read_dta)

names(valetal) <- sprintf("study%s", 1:4)
```


```{r warning = FALSE, message = FALSE}
natscape <- 
  list.files(file.path(base_path, "data/nationscape"), 
             pattern = "\\.dta", recursive = TRUE, full = TRUE) %>% 
  lapply(function(x) zap_labels(read_dta(x))) %>% 
  bind_rows(.id = "wave")

natsamp <- list.files(file.path(base_path, "data/nationscape"), 
             pattern = "\\.dta", recursive = TRUE, full = TRUE) %>% 
  last() %>% read_dta()
```
```{r}
natscape <-
  mutate(natscape,
         racialres2 = 
           cbind(racial_attitudes_generations, 
                 racial_attitudes_tryhard) %>% 
           apply(2, scale) %>% 
           rowMeans()
  )
```


```{r}
### input the constituent thermometer ratings
d <- d %>%
  mutate(black_therm_baseline = ifelse(blacks_t_baseline < 101, blacks_t_baseline / 100, NA),
         latino_therm_baseline = ifelse(latinos_t_baseline < 101, latinos_t_baseline / 100, NA),
         muslim_therm_baseline = ifelse(muslims_t_baseline < 101, muslims_t_baseline / 100, NA),
         gays_therm_baseline = ifelse(gays_t_baseline < 101, gays_t_baseline / 100, NA),
         white_therm_baseline = ifelse(whites_t_baseline < 101, whites_t_baseline / 100, NA),
         christian_therm_baseline = ifelse(christians_t_baseline < 101, christians_t_baseline / 100, NA))


d <- d %>% 
    mutate(
        # agree -> low resentment
        resent_slavery_baseline = recode(as.character(race_slave_baseline), 
                                     `Strongly Agree` = 1L, `Agree` = 2L,
                                     `Don't Know` = 3L,
                                     `Disagree` = 4L, `Strongly Disagree` = 5L,
                                     .default = NA_integer_),
        resent_deserve_baseline = recode(as.character(race_deservemore_baseline), 
                                     `Strongly Agree` = 1L, `Agree` = 2L,
                                     `Don't Know` = 3L,
                                     `Disagree` = 4L, `Strongly Disagree` = 5L,
                                     .default = NA_integer_),
        # agree -> high resentment
        resent_overcome_baseline = recode(as.character(race_overcome_baseline), 
                                      `Strongly Agree` = 5L, `Agree` = 4L,
                                      `Don't Know` = 3L,
                                      `Disagree` = 2L, `Strongly Disagree` = 1L,
                                      .default = NA_integer_),
        resent_try_baseline = recode(as.character(race_tryharder_baseline), 
                                 `Strongly Agree` = 5L, `Agree` = 4L,
                                 `Don't Know` = 3L,
                                 `Disagree` = 2L, `Strongly Disagree` = 1L,
                                 .default = NA_integer_),
        
        racial_resent_baseline = cbind(resent_slavery_baseline, resent_deserve_baseline, 
                                       resent_overcome_baseline, resent_try_baseline) %>% 
          apply(2, scale) %>% 
          rowMeans(.)
    ) %>% 
    mutate(
        # agree -> low resentment
        resent_slavery_2016 = recode(as.character(race_slave_2016), 
                                `Strongly Agree` = 1L, `Agree` = 2L,
                                `Don't Know` = 3L,
                                `Disagree` = 4L, `Strongly Disagree` = 5L,
                                .default = NA_integer_),
        resent_deserve_2016 = recode(as.character(race_deservemore_2016), 
                                `Strongly Agree` = 1L, `Agree` = 2L,
                                `Don't Know` = 3L,
                                `Disagree` = 4L, `Strongly Disagree` = 5L,
                                .default = NA_integer_),
        # agree -> high resentment
        resent_overcome_2016 = recode(as.character(race_overcome_2016), 
                                 `Strongly Agree` = 5L, `Agree` = 4L,
                                 `Don't Know` = 3L,
                                 `Disagree` = 2L, `Strongly Disagree` = 1L,
                                 .default = NA_integer_),
        resent_try_2016 = recode(as.character(race_tryharder_2016), 
                            `Strongly Agree` = 5L, `Agree` = 4L,
                            `Don't Know` = 3L,
                            `Disagree` = 2L, `Strongly Disagree` = 1L,
                            .default = NA_integer_),
        
        racial_resent_2016 = cbind(resent_slavery_2016, resent_deserve_2016, 
                                   resent_overcome_2016, resent_try_2016) %>% 
            apply(2, scale) %>% 
            rowMeans()
    )
```


# Racial resentment plots {.tabset}
## VOTER panel
```{r}
d %>% filter(!is.na(weight_panel), !is.na(racial_resent_2016),
             !is.na(racial_resent_baseline)) %>%
  nrow()
```


```{r}
d %>% filter(!is.na(weight_panel)) %>% 
    ggplot() + 
    aes(weight = weight_panel) +
    geom_density(aes(x = racial_resent_baseline,
                     color = "Baseline")) +
    geom_density(aes(x = racial_resent_2016,
                     color = "2016")) +
  scale_color_manual(values = c(Baseline = "black", `2016` = "red")) +
  coord_cartesian(xlim = c(-2, 2), ylim = c(0, 1))

```

## ANES {.tabset}

### Recent years
```{r}
anes %>% filter(year > 2008, !is.na(weight), race == "white") %>% 
  split(., .$year) %>% sapply(nrow)
```

```{r}
anes %>% filter(year > 2008, !is.na(weight), race == "white") %>% 
    ggplot() + 
    aes(weight = weight) +
    geom_density(aes(x = racialres, color = year)) +
    # scale_color_manual(values = c(`2012` = "black", `2016` = "red", `2020`)) +
    coord_cartesian(xlim = c(-2, 2), ylim = c(0, 1))
```


### Recent, by party

```{r fig.height = 10}
p <- 
  filter(anes, year > 2008, !is.na(weight), race == "white",
       pid3 %in% c("dem", "rep")) %>%
  ggplot() +
  aes(x = racialres, y = year, fill = pid3) +
  geom_density_ridges(alpha = 0.2) +
  scale_fill_manual(breaks = c("rep", "dem"),
                    labels = c("GOP", "Dem"),
                    values = c("red", "blue")) +
  coord_cartesian(xlim = c(-2, 2)) +
  labs(x = "Racial resentment", y = "Year", fill = "Party") +
  cowplot::theme_cowplot()

p
```


```{r}
ggsave(plot = p, file.path(base_path, "figs/DensityRacialResParty.pdf"), 
       height = 4, width = 7)
```


### All years

```{r fig.height = 10}
filter(anes, !is.na(weight) & race == "white") %>% 
  ggplot() +
  aes(x = racialres, y = year) +
  geom_density_ridges() +
  coord_cartesian(xlim = c(-2, 2))
```



## Valentino et al. 2010-2012

The replication data contained a pre-made racial resentment/symbolic racism variable (based on the standard 4-question battery on the ANES), but not the individual responses. So I believe this version is averaging across questions, not taking a z-score.

> "In all four studies, we sampled whites only, since racial priming theory has focused mostly on the majority group’s response to racialized campaign rhetoric. The first three studies were fielded online using YouGov/Polimetrix. The fourth was contracted through Knowledge Networks. We used two firms to ensure that particular sampling strategies were not responsible for any pattern of findings we discovered. Study 1 employed a sample of 2,394 voting-age Americans from July 16 to August 8, 2010. Studies 2 and 3 were smaller, with 234 and 321 white respondents, respectively. Finally, study 4 employed another large sample of 3,114 white respondents. Response rates for all studies can be found in appendix A22."

```{r}
sapply(valetal, nrow)
```

```{r}
valetal %>% bind_rows(.id = "study") %>% 
  ggplot() +
  aes(x = symrac01) +
  geom_density() +
  facet_wrap("study")
```


## CCES 2012
Can't find racial resentment questions in more recent years. CCES asks only two of the questions.

```{r}
cces %>% filter(!is.na(racialres2), !is.na(weight)) %>% nrow()
```


```{r}
cces %>% 
    ggplot() + 
    aes(weight = weight) +
    geom_density(aes(x = racialres2, color = year),
                 alpha = 0.5) +
  coord_cartesian(xlim = c(-2, 2), ylim = c(0, 0.7))

```

## Nationscape 2020
Only two of the standard racial resentment questions.

```{r}
natscape %>% filter(!is.na(racialres2), !is.na(weight)) %>% nrow()
```

```{r}
natscape %>% 
    ggplot() + 
    aes(weight = weight) +
    geom_density(aes(x = racialres2))
```




Other two questions ask about inter-racial dating/marrying.


# Black FT plots

Check out the left tail in 2020!

```{r}
filter(anes, !is.na(therm_black)) %>% 
  ggplot() +
  aes(x = therm_black, y = year) +
  geom_density_ridges()
```

```{r}
filter(anes, year == 2020) %>% split(., .$mode) %>% 
  lapply(function(x) qplot(x$racialres, geom = "density"))
```

